﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using Core.Base;
using ERP.BusinessEntities;

namespace MainApp.Modules.SO
{
    public class SOModule: BaseModule
    {
        public SOModule()
        {
            this.Name = "SO";
            entity = new SOEntity();
            Init();
        }

        public override void ActionSave()
        {
            ARSOsInfo SO = (ARSOsInfo)entity.MainObject;
            //base.ActionSave();
        }

        #region Override
        public override void CalcObject(object MainObj, string strFieldName)
        {
            base.CalcObject(MainObj, strFieldName);
            ARSOsInfo SO = (ARSOsInfo)entity.MainObject;
            if (SO == null) return;

            if (strFieldName == "FK_GLBPs_ID")
            {
                GLBPsInfo BP = (GLBPsInfo)AppContext.BPCtrl.GetObjectById(SO.FK_GLBPs_ID);
                if (BP != null)
                {
                    SO.BillToName = BP.BillToName;
                    SO.BillToPhone = BP.BillToPhone;
                    SO.BillToAddress = BP.BillToAddress;

                    SO.ShipToName = BP.ShipToName;
                    SO.ShipToPhone = BP.ShipToPhone;
                    SO.ShipToAddress = BP.ShipToAddress;

                    UpdateBinding(entity.strMainTable);
                }
            }
        }

        public override void CalcItem_FK_ICProducts_ID(Object objItem, ICProductsInfo Product)
        {
            AppContext.SetProperty(objItem, AppContext.cstFKStock, Product.FK_ICStocks_ID);
            AppContext.SetProperty(objItem, AppContext.cstFKStockUOM, Product.FK_ICStockUOMs_ID);
            AppContext.SetProperty(objItem, AppContext.cstFKTaxCode, Product.FK_TxTaxCodes_OutVATID);
            AppContext.SetProperty(objItem, AppContext.cstFUnitPrice, Product.SaleUnitPrice);
        } 
        #endregion

        #region Validating
        public override string ObjectValidating(object obj)
        {
            String strErrorText = String.Empty;
            if (obj == null) return strErrorText;

            if (obj is ARSOItemsInfo)
            {
                if (((ARSOItemsInfo)obj).FK_ICProducts_ID == 0)
                    strErrorText = "Vui lòng chọn sản phẩm";
            }

            return strErrorText;
        }

        public override string FieldValidating(object obj, String strFieldName, Object objNewValue)
        {
            String strErrorText = String.Empty;
            if (obj == null) return strErrorText;

            if (obj is ARSOItemsInfo && strFieldName == "FQty")
            {
                int iFQty = Convert.ToInt32(objNewValue);
                if (iFQty <=0 )
                    strErrorText = "Số lượng phải lớn hơn 0";
            }

            return strErrorText;
        }
        #endregion
    }
}
